package com.example.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasGetter("createTime")&&metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        if(metaObject.hasGetter("updateTime")&&metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if(metaObject.hasGetter("createUser")&&metaObject.hasSetter("createUser")){
            metaObject.setValue("createUser", BaseContext.getThreadLocal());
        }
        if(metaObject.hasGetter("updateUser")&&metaObject.hasSetter("updateUser")){
            metaObject.setValue("updateUser", BaseContext.getThreadLocal());
        }

//        metaObject.setValue("updateTime", LocalDateTime.now());
//        metaObject.setValue("createUser",BaseContext.getThreadLocal());
//        metaObject.setValue("updateUser",BaseContext.getThreadLocal());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasGetter("updateTime")&&metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if(metaObject.hasGetter("updateUser")&&metaObject.hasSetter("updateUser")){
            metaObject.setValue("updateUser", BaseContext.getThreadLocal());
        }

//        metaObject.setValue("updateTime", LocalDateTime.now());
//        metaObject.setValue("updateUser",BaseContext.getThreadLocal());
    }
}
